<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="任务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="任务">
      </el-table-column>
      <el-table-column
        prop="flowname"
        header-align="center"
        align="center"
        label="流程名">
      </el-table-column>
      <el-table-column
        prop="starter"
        header-align="center"
        align="center"
        label="申请人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="200"
        label="任务时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="dealHandle(scope.row.starter, scope.row.definitionkey, scope.row.businesskey, scope.row.id)">处理</el-button>
          <el-button type="text" size="small" @click="bpmnViewHandle(scope.row.processinstanceid)">查看状态</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <leave-request v-if="leaveRequestVisible" ref="leaveRequest" @refreshDataList="getDataList"></leave-request>
    <!-- 弹窗, 查看模型 -->
    <bpmn-view v-if="viewVisible" ref="bpmnView"></bpmn-view>
  </div>
</template>

<script>
  import LeaveRequest from './leaverequest-add-or-update'
  import BpmnView from './flow-view'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        leaveRequestVisible: false,
        viewVisible: false
      }
    },
    components: {
      LeaveRequest,
      BpmnView
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (page) {
        this.dataListLoading = true
        if (page) this.pageIndex = page
        this.$http({
          url: this.$http.adornUrl('/flow/process/todo'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.totalPage = data.data.totalCount
            this.dataList = data.data.list
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 处理任务
      dealHandle (starter, definitionkey, businesskey, taskid) {
        console.log(taskid)
        if (definitionkey === 'Process_leave') {
          this.leaveRequestHandle(businesskey, starter === this.$store.state.user.name1, taskid)
        }
      },
      // 查看流程图
      bpmnViewHandle (id) {
        this.viewVisible = true
        this.$nextTick(() => {
          this.$refs.bpmnView.init(id)
        })
      },
      // 新增 / 修改
      leaveRequestHandle (id, updated, taskid) {
        this.leaveRequestVisible = true
        this.$nextTick(() => {
          this.$refs.leaveRequest.init(id, updated, taskid)
        })
      }
    }
  }
</script>
